#include "btform.h"
#include "ui_btform.h"
#include <QLabel>
#include <QPixmap>
#include <QStyle>
#include <QMouseEvent>
#include <QTimer>

BtForm::BtForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::BtForm)
{
    ui->setupUi(this);

    ui->lineBox->hide();

    // 设置 line1 的动画效果
    animal_line1 = new QPropertyAnimation(ui->line1, "geometry", this);
    animal_line1->setDuration(800);
    animal_line1->setKeyValueAt(0, QRect(6, 30, 2, 0));  // 起始帧
    animal_line1->setKeyValueAt(0.5, QRect(6, 10, 2, 20)); // 中间帧
    animal_line1->setKeyValueAt(1, QRect(6, 30, 2, 0)); // 结束帧
    animal_line1->setLoopCount(-1);
    animal_line1->setEasingCurve(QEasingCurve::OutBounce); // 弹跳效果
    animal_line1->start();

    // 设置 line2 的动画效果
    animal_line2 = new QPropertyAnimation(ui->line2, "geometry", this);
    animal_line2->setDuration(900);
    animal_line2->setKeyValueAt(0, QRect(12, 30, 2, 0));  // 起始帧
    animal_line2->setKeyValueAt(0.5, QRect(12, 10, 2, 20)); // 中间帧
    animal_line2->setKeyValueAt(1, QRect(12, 30, 2, 0)); // 结束帧
    animal_line2->setLoopCount(-1);
    animal_line2->setEasingCurve(QEasingCurve::OutBounce); // 弹跳效果
    animal_line2->start();

    // 设置 line3 的动画效果
    animal_line3 = new QPropertyAnimation(ui->line3, "geometry", this);
    animal_line3->setDuration(1000);
    animal_line3->setKeyValueAt(0, QRect(17, 30, 2, 0));  // 起始帧
    animal_line3->setKeyValueAt(0.5, QRect(17, 10, 2, 20)); // 中间帧
    animal_line3->setKeyValueAt(1, QRect(17, 30, 2, 0)); // 结束帧
    animal_line3->setLoopCount(-1);
    animal_line3->setEasingCurve(QEasingCurve::OutBounce); // 弹跳效果
    animal_line3->start();

    // 设置 line4 的动画效果
    animal_line4 = new QPropertyAnimation(ui->line4, "geometry", this);
    animal_line4->setDuration(1100);
    animal_line4->setKeyValueAt(0, QRect(23, 30, 2, 0));  // 起始帧
    animal_line4->setKeyValueAt(0.5, QRect(23, 10, 2, 20)); // 中间帧
    animal_line4->setKeyValueAt(1, QRect(23, 30, 2, 0)); // 结束帧
    animal_line4->setLoopCount(-1);
    animal_line4->setEasingCurve(QEasingCurve::OutBounce); // 弹跳效果
    animal_line4->start();

}


BtForm::~BtForm(){
    delete ui;
}

void BtForm::seticon(const QString btIcon, const QString content, int id){
    // 加载图片
    QPixmap originalPixmap(btIcon);

    // 检查图片是否加载成功（可选）
    if (originalPixmap.isNull()) {
        qDebug() << "Failed to load icon:" << btIcon;
        return;
    }

    // 缩放图片为 10x10 像素，使用平滑插值算法
    QPixmap scaledPixmap = originalPixmap.scaled(
        27, 27,                   // 目标宽度和高度
        Qt::IgnoreAspectRatio,    // 忽略宽高比（强制缩放为 10x10）
        Qt::SmoothTransformation  // 使用平滑插值算法
        );

    // 设置到控件
    ui->btIcon->setPixmap(scaledPixmap);

    // 保存 mid（可选）
    this->pageId = id;

    // 可选：设置文本内容
    ui->btText->setText(content);
    QFont font("华文楷体", 12, QFont::Bold); // 字体家族、字号、粗细
    ui->btText->setFont(font);
}

void BtForm::mousePressEvent(QMouseEvent *event) {
    // 告诉编译器不要触发警告
    (void)event;
    if (event->button() == Qt::LeftButton) {
        selected = true;
        ui->btStyle->setStyleSheet("background-color: #FFE5E5; color: #333333;");
    }
    QWidget::mousePressEvent(event);
    emit btClick(this->pageId); // 发送⿏标点击信号
}

// 鼠标进入组件
void BtForm::enterEvent(QEnterEvent *event)
{
    Q_UNUSED(event);
    if (!selected) {
        ui->btStyle->setStyleSheet("background-color: rgb(228, 232, 246); color: black;");
    }
}

// 鼠标离开组件
void BtForm::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    if (!selected) {
        ui->btStyle->setStyleSheet("background-color: transparent;");
    }
}

void BtForm::showAnimal(){
    ui->btStyle->setStyleSheet("background-color: #FFE5E5; color: #333333;");
    selected = true;
    ui->lineBox->show();
}

void BtForm::clearBg()
{
    // if (selected) {
    //     // 恢复为 hover 状态颜色或 leave 状态颜色
    //     ui->btStyle->setStyleSheet("background-color: transparent; color: black;");
    //     selected = false;
    // }
    if (selected) {
        QPropertyAnimation *anim = new QPropertyAnimation(ui->btStyle, "styleSheet");
        anim->setDuration(200);
        anim->setStartValue("background-color: rgb(252, 61, 76); color: #F6F6F6;");
        anim->setEndValue("background-color: transparent; color: black;");
        anim->start(QAbstractAnimation::DeleteWhenStopped);
        ui->lineBox->hide();
        selected = false;
    }
}

int BtForm::getId(){
    return pageId;
}
